package repository

import (
	"cr.org/db"
	"cr.org/model"
	"fmt"
)

type otherRepository struct {
}

func (r otherRepository) FindAiDraw(arg model.SearchAiDraw, uid int64) (li []model.AiDrawT, err error) {
	err = db.GetDB().Model(&model.AiDrawT{}).Where("user_id = ?", uid).Order(fmt.Sprintf("%s %s", arg.OrderBy, arg.Sort)).
		Limit(arg.PageSize).Find(&li).Error
	return
}

func (r otherRepository) FindAiDrawPage(arg model.SearchAiDraw, uid int64) (li []model.AiDrawT, total int64, err error) {
	err = db.GetDB().Model(&model.AiDrawT{}).Where("user_id = ?", uid).Order(fmt.Sprintf("%s %s", arg.OrderBy, arg.Sort)).
		Limit(arg.PageSize).Offset((arg.Page - 1) * arg.PageSize).Count(&total).Find(&li).Error
	return
}

func (r otherRepository) FindAiModelsPage(arg model.SearchAiModels) (li []model.AiModelsT, total int64, err error) {
	err = db.GetDB().Model(&model.AiModelsT{}).
		Limit(arg.PageSize).Offset((arg.Page - 1) * arg.PageSize).Count(&total).Find(&li).Error
	return
}

func (r otherRepository) SaveAiDraw(m *model.AiDrawT) (err error) {
	err = db.GetDB().Save(m).Error
	return err
}

func (r otherRepository) FindAiDrawById(id int64) (m model.AiDrawT, err error) {
	err = db.GetDB().Model(&model.AiDrawT{}).Where("id = ? ", id).Find(&m).Error
	return
}

func (r otherRepository) SaveAiModels(m *model.AiModelsT) (err error) {
	err = db.GetDB().Save(m).Error
	return err
}

func (r otherRepository) FindAiModelsById(id int64) (m model.AiModelsT, err error) {
	err = db.GetDB().Model(&model.AiModelsT{}).Where("id = ? ", id).Find(&m).Error
	return
}

func (r otherRepository) CheckThumbsUp(uid, drawId int64) (m model.MapUserDrawThumbsUp, err error) {
	err = db.GetDB().Model(&model.MapUserDrawThumbsUp{}).
		Where("user_id =? and draw_id = ?", uid, drawId).Find(&m).Error
	return
}

func (r otherRepository) InsertThumbsUpMap(m model.MapUserDrawThumbsUp) (err error) {
	err = db.GetDB().Model(&model.MapUserDrawThumbsUp{}).Create(&m).Error
	return err
}
func (r otherRepository) DeleteThumbsUpMap(m model.MapUserDrawThumbsUp) (err error) {
	err = db.GetDB().Model(&model.MapUserDrawThumbsUp{}).Where("user_id =? and draw_id = ?", m.UserId, m.DrawId).Delete(m).Error
	return err
}
